// pages/caseReward/bankInfo/bankInfo.js
const app = getApp();
import util from '../../../utils/util.js';
import request from '../../../utils/config.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // userExt: {},
    bankInfo:{},
    username: '',
    mobile: '',
    // email: '',
    accountName: "",
    accountNo: "",
    openBankName: "",
    accountType: "个人"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      // userExt: app.globalData.userExt,
      username: app.globalData.bankInfo.userName,
      mobile: app.globalData.bankInfo.mobileNo,
      accountType: app.globalData.bankInfo.accountType == '1' ? "企业" : app.globalData.bankInfo.accountType == '0'?"个人":"个人",
      accountName: app.globalData.bankInfo.accountName ? app.globalData.bankInfo.accountName:"",
      accountNo: app.globalData.bankInfo.accountNo ? app.globalData.bankInfo.accountNo : "",
      openBankName: app.globalData.bankInfo.openBankName ? app.globalData.bankInfo.openBankName : "",
      bankInfo: app.globalData.bankInfo
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //触发姓名
  change_name: function (e) {
    this.setData({
      username: e.detail.value
    })
  },

  //手机号触发
  change_mobileNo: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  //账户名称
  change_accountName: function (e) {
    this.setData({
      accountName: e.detail.value
    })
  },

  //账号
  change_accountNo: function (e) {
    this.setData({
      accountNo: e.detail.value
    })
  },

  //开户行名称
  change_openBankName: function (e) {
    this.setData({
      openBankName: e.detail.value
    })
  },

  //显示账户类型
  showAccountType: function () {
    let _this = this;
    wx.showActionSheet({
      itemList: ['个人', '企业'],
      success: function (res) {
        let accountType = "个人";
        if (res.tapIndex == 0) {
          accountType = "个人";
        } else if (res.tapIndex == 1) {
          accountType = "企业";
        }
        _this.setData({
          accountType: accountType
        });
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  //确认提交
  submit: function () {
    if (!this.data.username) {
      wx.showToast({ title: '姓名不能为空', icon: 'none' })
      return;
    }
    if (!this.data.mobile) {
      wx.showToast({ title: '手机号不能为空', icon: 'none' })
      return;
    } else if (!util.isValidNo(this.data.mobile)) {
      wx.showToast({ title: '手机号格式不正确', icon: 'none' })
      return;
    }
    if (!this.data.accountName){
      wx.showToast({ title: '账户名称不能为空', icon: 'none' })
      return;
    }
    if (!this.data.accountNo) {
      wx.showToast({ title: '账号不能为空', icon: 'none' })
      return;
    } else if (!util._YHC_util.luhmCheck(this.data.accountNo)){
      wx.showToast({ title: '账号格式不正确', icon: 'none' })
      return;
    }
    if (!this.data.openBankName) {
      wx.showToast({ title: '开户行名称不能为空', icon: 'none' })
      return;
    }
    //存储bankInfo并返回
    let accountType = this.data.accountType == "个人"?'0':'1';
    app.globalData.bankInfo.userName = this.data.username;
    app.globalData.bankInfo.mobileNo = this.data.mobile;
    app.globalData.bankInfo.accountNo = this.data.accountNo;
    app.globalData.bankInfo.accountName = this.data.accountName;
    app.globalData.bankInfo.openBankName = this.data.openBankName;
    app.globalData.bankInfo.accountType = accountType;
   
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})